/*
 * @Description  : 
 * @Version      : 1.0
 * @Author       : Shiming
 * @Date         : 2022-01-25 16:03:45
 * @LastEditors  : Shiming
 * @LastEditTime : 2022-02-11 17:12:52
 * @FilePath     : \\tms-obc-web\\src\\app\\shared\\components\\dynamic-setting\\freight-table\\freight-table.component.ts
 * Copyright (C) 2022 huzhenhong. All rights reserved.
 */
import { Component, OnInit } from '@angular/core';
import { BaseService } from '@shared';

@Component({
  selector: 'app-freight-table',
  templateUrl: './freight-table.component.html',
  styleUrls: ['./freight-table.component.less']
})
export class FreightTableComponent implements OnInit {
  data: any[] = [];
  headers: any[] = [];

  formatterDollar = (value: number): string => `${value} （含）`;
  computeMode: any = {
    0: '总运价',
    1: '单公里运价'
  };
  constructor(public service: BaseService) {}

  ngOnInit(): void {
    this.loadHeaders();
    this.loadData();
  }

  loadHeaders() {
    this.service.request('/api/mdc/cuc/freightConfigItem/list').subscribe(res => {
      if (res) {
        this.headers = res;
      }
    });
  }

  loadData() {
    this.service.request('/api/mdc/cuc/freightConfig/list').subscribe(res => {
      if (res) {
        this.data = res;
      }
    });
  }

  /**
   * 修改结束车长
   * @param event 车长
   * @param i 下标
   */
  changeEndLength(event: any, i: number) {
    if (event <= this.headers[i].startLength) {
      this.headers[i].endLength = this.headers[i].startLength + 1;
      this.changeNextStartLength(event, i + 1);
      return;
    }
    this.headers[i].endLength = event;
    this.changeNextStartLength(event, i + 1);
  }

  /**
   * 修改结束公里数
   * @param event 车长
   * @param i 下标
   */
  changeEndKm(event: any, i: number) {
    if (event <= this.data[i].startKm) {
      this.data[i].endKm = this.data[i].startKm + 1;
      this.changeNextStartKm(event, i + 1);
      return;
    }
    this.data[i].endKm = event;
    this.changeNextStartKm(event, i + 1);
  }

  add() {
    console.log(this.data);

    const tem = this.data[this.data?.length - 1];
    if (tem && tem.endKm) {
      const list = this.headers.map(item => ({
        ewPrice: null,
        itemId: item.id,
        maxPrice: null
      }));
      this.data.push({
        computeMode: 1,
        configValue: list,
        endKm: '',
        startKm: tem.endKm
      });
      this.data = [...this.data];
    } else {
      this.service.msgSrv.warning('请填写完整公里数');
    }
  }

  deleteRow(index: number) {
    this.data = this.data.filter((d, i) => index !== i);
  }

  save() {
    this.service.request('/api/mdc/cuc/freightConfig/saveBatch', this.data).subscribe(res => {
      if (res) {
        console.log(res);
        this.service.msgSrv.success('修改成功');
        this.loadData();
      }
    });
  }

  /**
   * 遍历同步后置位车长
   * @param event  车长
   * @param i 下标
   */
  private changeNextStartLength(event: number, i: number) {
    if (this.headers[i]) {
      this.headers[i].startLength = event;
      if (this.headers[i].endLength <= event) {
        this.headers[i].endLength = this.headers[i].startLength + 0.5;
        this.changeNextStartLength(event + 0.5, i + 1);
      }
    }
  }

  /**
   * 遍历同步后置位公里数
   * @param event  车长
   * @param i 下标
   */
  private changeNextStartKm(event: number, i: number) {
    if (this.data[i]) {
      this.data[i].startKm = event;
      if (this.data[i].endKm <= event) {
        this.data[i].endKm = this.data[i].startKm + 1;
        this.changeNextStartKm(event + 1, i + 1);
      }
    }
  }
}
